{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4e4b2e2e",
   "metadata": {},
   "source": [
    "## 实践 宿舍人脸识别门禁系统"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c150d4e",
   "metadata": {},
   "source": [
    "* 1.创建FaceSet\n",
    "* 2.查询FaceSet\n",
    "* 3.存入face_token数据\n",
    "* 4.获取FaceSet信息数据\n",
    "* 5.人脸搜索\n",
    "* 6.人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aefbb7e8",
   "metadata": {},
   "source": [
    "### 1.创建FaceSet\n",
    "* 描述\n",
    "> 1.创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。             \n",
    "> 2.试用API Key可以创建 1000个FaceSet  ，正式API Key可以创建10000个FaceSet。          \n",
    "\n",
    "* 调用URL\n",
    "https://api-cn.faceplusplus.com/facepp/v3/faceset/create\n",
    "\n",
    "* 调用方法\n",
    "POST\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "6e0529ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "cb13dbba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>display_name</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸集合的名字，最长256个字符，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>可选</td>\n",
       "      <td>face_tokens</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>可选</td>\n",
       "      <td>user_data</td>\n",
       "      <td>String</td>\n",
       "      <td>自定义用户信息，不大于16 KB，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>可选</td>\n",
       "      <td>force_merge</td>\n",
       "      <td>Int</td>\n",
       "      <td>在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选           参数名      类型  \\\n",
       "0   必选       api_key  String   \n",
       "1   必选    api_secret  String   \n",
       "2   可选  display_name  String   \n",
       "3   可选      outer_id  String   \n",
       "4   可选          tags  String   \n",
       "5   可选   face_tokens  String   \n",
       "6   可选     user_data  String   \n",
       "7   可选   force_merge     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                  调用此 API 的 API Key  \n",
       "1                               调用此 API 的 API Secret  \n",
       "2                    人脸集合的名字，最长256个字符，不能包括字符^@,&=*'\"  \n",
       "3  账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...  \n",
       "4  FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...  \n",
       "5  人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...  \n",
       "6                    自定义用户信息，不大于16 KB，不能包括字符^@,&=*'\"  \n",
       "7  在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...  "
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Secret</span></p></td></tr><tr><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">display_name</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">人脸集合的名字，最长<span class=\"inline-comment-marker\" data-ref=\"9f098728-ac30-41a2-aab6-05b79f8a7d2e\">256</span>个字符，不能包括字符^@,&=*'\"</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255个字符，<span class=\"inline-comment-marker\" data-ref=\"63fc0fd1-cd44-4256-bab5-971a939d66e2\"><span class=\"inline-comment-marker\" data-ref=\"183dcb11-c22c-47f4-aa9c-2ba6f3c8210c\">不能包括字符^@,&=*'\"</span></span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长<span class=\"inline-comment-marker\" data-ref=\"1be760b6-f20d-4ff3-a086-b7f763abd84d\">255</span>个字符，多个 tag 用逗号分隔，每个 tag 不能包括字符^@,&=*'\"</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_tokens</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_token</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">user_data</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">自定义用户信息，不大于<span class=\"inline-comment-marker\" data-ref=\"f9edadb1-691d-4d80-933e-909096780f61\">16 KB，不能包括字符^@,&=*'\"</span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">force_merge</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_token 加入已经存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">0：不将 face_tokens 加入已存在的 FaceSet 中，直接返回 FACESET_EXIST 错误</span></p><p><span style=\"color: rgb(0,0,0);\">1：将 face_tokens 加入已存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">默认值为0</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "7dbd9705",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "1b3fd6a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "# 5.请求参数\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "e0ff2386",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "7292760a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3c78186e7e2d9b7c20b32d16d6f00f09',\n",
       " 'time_used': 170,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648743936,952dc5ea-a469-434b-9b64-9fbdd139921b',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "1e0df8ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"faceset_token\": \"3c78186e7e2d9b7c20b32d16d6f00f09\", \"time_used\": 170, \"face_count\": 0, \"face_added\": 0, \"request_id\": \"1648743936,952dc5ea-a469-434b-9b64-9fbdd139921b\", \"outer_id\": \"\", \"failure_detail\": []}'"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2f70d62",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "53c077bd",
   "metadata": {},
   "source": [
    "##  2.查询人脸库\n",
    "* 描述\n",
    "> 1.获取某一 API Key 下的 FaceSet 列表及其 faceset_token、outer_id、display_name 和 tags 等信息。                             \n",
    "> 2.注意：2017年8月16日后，调用本接口将不会一次性返回全量的 FaceSet。单次查询最多返回 100 个 FaceSet。                  \n",
    "> 3.如需获取全量数据，需要配合使用 start 和 next 参数。请尽快修改调整您的程序。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a72f112",
   "metadata": {},
   "source": [
    "*  请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "58d74011",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>包含需要查询的FaceSet标签的字符串，用逗号分隔</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>start</td>\n",
       "      <td>Int</td>\n",
       "      <td>一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选         参数名      类型                                               参数说明\n",
       "0   必选     api_key  String                                     调用此API的API Key\n",
       "1   必选  api_secret  String                                  调用此API的API Secret\n",
       "2   可选        tags  String                         包含需要查询的FaceSet标签的字符串，用逗号分隔\n",
       "3   可选       start     Int  一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号..."
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Secret</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">包含需要查询的FaceSet标签的字符串，用逗号分隔</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">start</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">一个数字 n，表示开始返回的 faceset_token 在传入的 API Key 下的序号。</span></p><p><span style=\"color: rgb(0,0,0);\">通过传入数字 n，可以控制本 API 从第 n 个 faceset_token 开始返回。返回的 faceset_token 按照创建时间排序。每次返回<span class=\"s1\">1000</span>个<span class=\"s1\">FaceSets</span>。</span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">默认值为1。</span></p><p class=\"p1\"><span style=\"color: rgb(255,0,0);\">注意：2017年8月16日后，每次返回的 FaceSet 数量上限从 1000 改为 100。并且您可以输入上一次请求本 API 返回的 next 值，用以获得接下来的 100 个 faceset_token。请尽快修改调整您的程序。</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "683d7b30",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "122fd79d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_getfaceset_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "# 5.请求参数\n",
    "r = requests.post(FaceSet_getfaceset_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "9ce3d433",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "593cdf7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 91,\n",
       " 'facesets': [{'faceset_token': 'bd4229e2eb164ec84eb0c787b61ade5d',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'be5b7192e88f85f5b5efcc8e8c5a19af',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'd3c227a725cf60c313313a5a3cbf1a69',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '28b58b983a793d85aa3e66b0a6c3f426',\n",
       "   'outer_id': 'NFUSD00',\n",
       "   'display_name': '广州南方学院学生宿舍楼',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '082e7ceb1b6b25d08a55b4414f97cae0',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '3c78186e7e2d9b7c20b32d16d6f00f09',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648743942,546ace58-5f18-4230-912c-ee18ade54222'}"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "2f2039b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"time_used\": 91, \"facesets\": [{\"faceset_token\": \"bd4229e2eb164ec84eb0c787b61ade5d\", \"outer_id\": \"\", \"display_name\": \"\", \"tags\": \"\"}, {\"faceset_token\": \"be5b7192e88f85f5b5efcc8e8c5a19af\", \"outer_id\": \"\", \"display_name\": \"\", \"tags\": \"\"}, {\"faceset_token\": \"d3c227a725cf60c313313a5a3cbf1a69\", \"outer_id\": \"\", \"display_name\": \"\", \"tags\": \"\"}, {\"faceset_token\": \"28b58b983a793d85aa3e66b0a6c3f426\", \"outer_id\": \"NFUSD00\", \"display_name\": \"\\\\u5e7f\\\\u5dde\\\\u5357\\\\u65b9\\\\u5b66\\\\u9662\\\\u5b66\\\\u751f\\\\u5bbf\\\\u820d\\\\u697c\", \"tags\": \"\"}, {\"faceset_token\": \"082e7ceb1b6b25d08a55b4414f97cae0\", \"outer_id\": \"\", \"display_name\": \"\", \"tags\": \"\"}, {\"faceset_token\": \"3c78186e7e2d9b7c20b32d16d6f00f09\", \"outer_id\": \"\", \"display_name\": \"\", \"tags\": \"\"}], \"request_id\": \"1648743942,546ace58-5f18-4230-912c-ee18ade54222\"}'"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df486d74",
   "metadata": {},
   "source": [
    "## 3.更新人脸库信息 API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "64f96229",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "411cc4f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "update_FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    'faceset_token':faceset_token,\n",
    "    \"new_outer_id\":\"NFUSD00\",\n",
    "    \"display_name\":\"广州南方学院学生宿舍楼\",\n",
    "    \"user_data\":\"59人\",\n",
    "    \n",
    "}\n",
    "# 5.请求参数\n",
    "r = requests.post(url = update_FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "60eed37f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 81,\n",
       " 'error_message': 'NEW_OUTER_ID_EXIST',\n",
       " 'request_id': '1648743945,c58f5e35-5080-4dd9-8063-9546cc46e09f'}"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3faafaa",
   "metadata": {},
   "source": [
    "## 4.检查数据集合信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "bdaa4b2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "740ae90e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_getfaceset_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "   \"outer_id\": \"NFUSD00\"   \n",
    "}\n",
    "# 5.请求参数\n",
    "r = requests.post(FaceSet_getfaceset_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "07d4d3ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 77,\n",
       " 'facesets': [{'faceset_token': 'bd4229e2eb164ec84eb0c787b61ade5d',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'be5b7192e88f85f5b5efcc8e8c5a19af',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'd3c227a725cf60c313313a5a3cbf1a69',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '28b58b983a793d85aa3e66b0a6c3f426',\n",
       "   'outer_id': 'NFUSD00',\n",
       "   'display_name': '广州南方学院学生宿舍楼',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '082e7ceb1b6b25d08a55b4414f97cae0',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '3c78186e7e2d9b7c20b32d16d6f00f09',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648743949,c42e161e-6030-4c79-ab5b-dc41538ae9c1'}"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8181e4dd",
   "metadata": {},
   "source": [
    "## 5.人脸检测，获取人脸token  （作业1：尝试创建detect.py 并实现功能调用）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "07cb410e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "9cc10dd0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\lenovo\\study\\api\\git\\api\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help (detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "1b247238",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648743951,feb7ec7e-1a6b-44da-a249-a51b3b658ce5',\n",
       " 'time_used': 107,\n",
       " 'faces': [{'face_token': '300e6db857cec8f2c0ab1020b59a6d82',\n",
       "   'face_rectangle': {'top': 143, 'left': 265, 'width': 129, 'height': 129}}],\n",
       " 'image_id': 'rNy3Z+S926e3YMVA/OuAKw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_Secret,\"Liu_01.jpeg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5eb1c15a",
   "metadata": {},
   "source": [
    "## 6.人脸存放"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "8b5fd7fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "ac404121",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_getfaceset_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "   \"outer_id\": \"NFUSD00\",\n",
    "    \"face_tokens\":\"\"\n",
    "}\n",
    "# 5.请求参数\n",
    "r = requests.post(FaceSet_getfaceset_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "70071734",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '28b58b983a793d85aa3e66b0a6c3f426',\n",
       " 'time_used': 78,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648743953,620706b1-fb9e-443f-853b-b72758d8c9e8',\n",
       " 'outer_id': 'NFUSD00',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "39819b9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"faceset_token\": \"28b58b983a793d85aa3e66b0a6c3f426\", \"time_used\": 78, \"face_count\": 0, \"face_added\": 0, \"request_id\": \"1648743953,620706b1-fb9e-443f-853b-b72758d8c9e8\", \"outer_id\": \"NFUSD00\", \"failure_detail\": []}'"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "3b4edc3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b516eaa1",
   "metadata": {},
   "source": [
    "## 作业2 尝试创建analyze.py\\campare.py\\search.py，并实现功能调用\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "2703b198",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "9f048373",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_analyze接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\lenovo\\study\\api\\git\\api\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help (analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "b505cbb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 63,\n",
       " 'error_message': 'VOID_REQUEST',\n",
       " 'request_id': '1648743959,6697015c-291c-41e9-af0f-984e41593aef'}"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_Key,API_Secret,\"Liu_01.jpeg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "1c221ed3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "f99716e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_compare接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\lenovo\\study\\api\\git\\api\\compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help (compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "e43559a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648743998,a48d5c0e-3902-4a48-b87f-5a1f0a3ca105',\n",
       " 'time_used': 76,\n",
       " 'error_message': 'MISSING_ARGUMENTS: face_token1, image_base64_1, image_url1, image_file1'}"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare.face_compare(API_Key,API_Secret,\"Liu_01.jpeg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "5bb1c726",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "a05081c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module search:\n",
      "\n",
      "NAME\n",
      "    search\n",
      "\n",
      "FUNCTIONS\n",
      "    face_search(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_search接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\lenovo\\study\\api\\git\\api\\search.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "7a335ff3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 61,\n",
       " 'error_message': 'MISSING_ARGUMENTS: faceset_token, outer_id',\n",
       " 'request_id': '1648751279,8f1b554a-1d10-49b8-89d5-541f55d2fe44'}"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_Key,API_Secret,\"Liu_01.jpeg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cef2a4f",
   "metadata": {},
   "source": [
    "## 作业3 尝试存放宿舍4位同学的人脸信息到 人脸数据集（XXX栋XXX宿舍）:                    \n",
    "> 完善faceset信息                                       \n",
    "> 实现人脸搜索和人脸对比，打印结果：是本人 OR 不是本人                                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d9863739",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8257efc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_getfaceset_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_Key = \"cH5_BCI4sJK8WFbnKb5rtndLupsJqbKC\"\n",
    "API_Secret = \"zCF9CoeKabTkOMH125XoLllsK_110UV4\"\n",
    "# 3.请求方法： POST\n",
    "\n",
    "# 4.准备payload：酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url1\":\"https://image.so.com/view?q=%E8%8F%85%E7%94%B0%E5%B0%86%E6%99%96&src=tab_www&correct=%E8%8F%85%E7%94%B0%E5%B0%86%E6%99%96&ancestor=list&cmsid=019bea2f234fae899cdd3a02852d2abc&cmras=0&cn=0&gn=0&kn=50&crn=0&bxn=20&fsn=130&cuben=0&pornn=0&manun=45&adstar=0&clw=254#id=bf6c2c55e2d1936359b5695db1c275b5&currsn=0&ps=127&pc=127\",\n",
    "    \"image_url2\":\"https://image.so.com/view?q=%E8%8F%85%E7%94%B0%E5%B0%86%E6%99%96&src=tab_www&correct=%E8%8F%85%E7%94%B0%E5%B0%86%E6%99%96&ancestor=list&cmsid=019bea2f234fae899cdd3a02852d2abc&cmras=0&cn=0&gn=0&kn=50&crn=0&bxn=20&fsn=130&cuben=0&pornn=0&manun=45&adstar=0&clw=254#id=d1ee8c9c01246b82dbce3d4185113edf&currsn=0&ps=127&pc=127\"\n",
    "   \n",
    "}\n",
    "# 5.请求参数\n",
    "r = requests.post(FaceSet_getfaceset_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "477be1c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1649069823,e99f763a-24f6-46b6-bd4e-a5787254597f\",\"time_used\":274,\"error_message\":\"IMAGE_ERROR_UNSUPPORTED_FORMAT: image_url1\"}\\n'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2eb1da3c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1649069823,e99f763a-24f6-46b6-bd4e-a5787254597f',\n",
       " 'time_used': 274,\n",
       " 'error_message': 'IMAGE_ERROR_UNSUPPORTED_FORMAT: image_url1'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d88940e7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
